package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.commom.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.stereotype.Service;

/*
@date 2022年07月30日18:24
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements CategoryService {
   @Autowired
    private SetmealService setmealService;
    @Autowired
   private DishService dishService;

    /**
     * 根据id删除,删除之前进行判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        //查询菜品中是否关联了这个分类
        LambdaQueryWrapper<Dish> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Dish::getCategoryId, id);
        int count = dishService.count(lqw1);
        if (count > 0) {
            throw new CustomException("已经关联菜品,不容许删除!");
        }

        //查询套餐中是否关联了这个分类
        LambdaQueryWrapper<Setmeal> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Setmeal::getCategoryId, id);
        int count1 = setmealService.count(lqw2);
        if (count1 > 0) {
            throw new CustomException("已经关联套餐,不容许删除!");
        }
        //正常删除分类
        removeById(id);
    }
}
